//Javascript for Nepomuk Thunderbird
//get the path of current profile
    var pfPath = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsILocalFile).path;    

    //The Identity of mail
    var mymsguri=window.opener.TagMail.mailURI;
    
    function enableNext(){
        document.getElementById("Nepomuk-ff-comment").canAdvance="true";
    }
   
    function getMailURI(event)
    {
        document.getElementById("labelMailURI").setAttribute("value",mymsguri);
    }

    function closeWizard(event)
    {
        window.close();
    }

    function checkInput(){
        document.getElementById("Nepomuk-tb-comment").canAdvance = (document.getElementById("textcomment").value!="");
    }

    function clearText(){
        var tagtext=document.getElementById("textcomment");
        tagtext.setAttribute("value","")
    }

    function tagSummary(){          
        //Get the input
        var commentText = document.getElementById("textcomment").value;

        document.getElementById("labComment").setAttribute("value",commentText);
    }

        //Save the Comment

      function saveComment(){
                // create an nsILocalFile for the executable
        var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath("/usr/bin/python");

        // create an nsIProcess
        var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
        process.init(file);
       
    var mail2com = mymsguri;
        var text2save = document.getElementById("textcomment").value;

        // Run the process.
        // If first param is true, calling thread will be blocked until
        // called process terminates.
        // Second and third params are used to pass command-line arguments
        // to the process.
    
    var scriptPath = pfPath+"/extensions/thunderbird@nepomuk.org/chrome/content/scripts/python_calls.py";

	filePath=pfPath+"/extensions/thunderbird@nepomuk.org/chrome/content";
        var args = [scriptPath, filePath, "setDescription", mail2com, text2save];
        process.run(true, args, args.length);
      }

      function removeComment(){
                // create an nsILocalFile for the executable
        var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath("/usr/bin/python");

        // create an nsIProcess
        var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
        process.init(file);
      
        var mail2com = mymsguri;

        // Run the process.
        // If first param is true, calling thread will be blocked until
        // called process terminates.
        // Second and third params are used to pass command-line arguments
        // to the process.
   
        var scriptPath = pfPath+"/extensions/thunderbird@nepomuk.org/chrome/content/scripts/python_calls.py";

	filePath=pfPath+"/extensions/thunderbird@nepomuk.org/chrome/content";
        var args = [scriptPath, filePath, "removeDescription", mail2com];
        process.run(true, args, args.length);
      }
      //get comment
      function getComment(){
                // create an nsILocalFile for the executable
        var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath("/usr/bin/python");

        // create an nsIProcess
        var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
        process.init(file);
      
        var mail2com = mymsguri;

        // Run the process.
        // If first param is true, calling thread will be blocked until
        // called process terminates.
        // Second and third params are used to pass command-line arguments
        // to the process.
   
        var scriptPath = pfPath+"/extensions/thunderbird@nepomuk.org/chrome/content/scripts/python_calls.py";

	filePath=pfPath+"/extensions/thunderbird@nepomuk.org/chrome/content";
        var args = [scriptPath, filePath, "getDescriptionOf", mail2com];
        process.run(true, args, args.length);

	//support file path on any platform
	var commentfile = "extensions/thunderbird@nepomuk.org/chrome/content/description.txt";
 
	try {
	  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
	  alert("Permission to save file was denied.");
	}
	// get the path to the user's home (profile) directory
	const DIR_SERVICE = new Components.Constructor("@mozilla.org/file/directory_service;1","nsIProperties");
	try {
	  path=(new DIR_SERVICE()).get("ProfD", Components.interfaces.nsIFile).path;
	} catch (e) {
	  alert("error");
	}
	// determine the file-separator
	if (path.search(/\\/) != -1) {
	  path = path + "\\";
	} else {
	  path = path + "/";
	}
	commentfile = path+commentfile;
	document.getElementById('showcomment').value = commentfile;



	//read the content of the file

	try {
	  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
	  alert("Permission to read file was denied.");
	}
	var file = Components.classes["@mozilla.org/file/local;1"]
	  .createInstance(Components.interfaces.nsILocalFile);
	file.initWithPath( commentfile );
	if ( file.exists() == false ) {
	  alert("File does not exist");
	}
	var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
	  .createInstance( Components.interfaces.nsIFileInputStream );
	is.init( file,0x01, 00004, null);
	var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
	  .createInstance( Components.interfaces.nsIScriptableInputStream );
	sis.init( is );
	var output = sis.read( sis.available() );
	document.getElementById('showcomment').value = output;
	//document.getElementById('textcomment').value = output;
      }

    function wizardFinish(){
        alert('Well Done');
    }